/*!
 * TidyBlog 2.0
 * User create & edit page - v1.0 (2013-10-06T11:34:56+0800)
 * https://code.google.com/p/tidyblog/ | Released under MIT license
 */
define(function(require, exports, module) { 'use strict';

var $ = require('dom/1.0.x/'),
	Validator = require('validator/1.0.x/'),
	util = require('/common/util/1.0.x/');

var steps = [
	{ fields: 'Nickname', message: '昵称不能为空' },
	{ fields: 'Password', rule: 'minLength:6', message: '密码长度不能小于6' },
	{
		fields: 'Password Password2',
		rule: function(pwd, pwd2) {
			return !pwd2[0] ? '请再次输入密码' : (
				pwd[0] === pwd2[0] ? true : '密码不一致'
			);
		}
	},
	{ fields: 'QQ', rule: 'isQQ', message: 'QQ号码必须由数字组成', required: false },
	{ fields: 'Email', rule: 'isEmail', required: false }
];

if ($('#UserName').length) {
	// 用户名不可修改，只有创建页面需要填写
	steps.unshift({
		fields: 'UserName',
		rule: function(val) { return /^\w{2,20}$/.test(val); },
		message: '用户名必须由2-20位字母、数字或下划线组成'
	});
} else {
	// 编辑资料页面中，密码允许不填（维持原密码）
	steps[1].required = steps[2].required = false;
	steps[2].rule = function(pwd, pwd2) {
		return pwd[0] === pwd2[0] ? true : '密码不一致';
	};
}

new Validator({
	form: $('form'),
	steps: steps,
	events: {
		submiterror: util.onSubmitError
	}
});

});